﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace CPAIS.Web.AjaxHandlers
{
    public abstract class AjaxHandler
    {
        #region Variables in AjaxHandler
        protected HttpContext Context;
        protected HttpApplicationState Application;
        protected HttpSessionState Session;
        protected HttpRequest Request;
        protected HttpResponse Response;
        protected NameValueCollection Params;
        protected JavaScriptSerializer serializer;
        #endregion

        public AjaxHandler(HttpContext context)
        {
            this.Context = context;
            this.Application = context.Application;
            this.Session = context.Session;
            this.Request = context.Request;
            this.Response = context.Response;
            this.Params = context.Request.Params;
            this.serializer = new JavaScriptSerializer();
            this.serializer.MaxJsonLength = int.MaxValue >> 2;
        }

        public DSManager DSMgr
        {
            get
            {
                return Application[Constants.DSManager] as DSManager;
            }
        }

        public MLManager MLMgr
        {
            get
            {
                return Session[Constants.MLManager] as MLManager;
            }
        }

        protected void WriteJson(object obj)
        {
            string json = serializer.Serialize(obj);
            Response.Write(json);
        }

        protected void WriteText(string text)
        {
            Response.Write(text);
        }
    }
}